% Compute Table 1 in Ritschl, Sarferaz, and Uebele (2015)
clear all
clc

addpath('funcs');
addpath('scrpts');
%% Prepare data and results

% Load and save data for plots
nipa_level      = load('data/us_gdp_1929_2008.txt');
nipa_loglevel   = log(nipa_level(2:end-2,2));
data_pre1930    = load('data/us_balke_gordon_romer_nominal_real.txt');
romer_loglevel  = log(data_pre1930(:,4));
bg_loglevel     = log(data_pre1930(:,2));
gdp_loglevel    = [romer_loglevel; nipa_loglevel];

cpi_level       = load('data/us_cpi_1875_2007.txt');
cpi_loglevel    = log(cpi_level(1:end-1));

[gdp_trend, gdp]    =   hpfilter(gdp_loglevel,6.25);
[cpi_trend, cpi]    =   hpfilter(cpi_loglevel,6.25);
[romer_trend, romer]  =   hpfilter(romer_loglevel,6.25);
[bg_trend, bg]     =   hpfilter(bg_loglevel,6.25);

std_nipa        = std(gdp(78:end));
std_cpi         = std(cpi(72:end));
std_bg          = std(bg);

save data/data_gdp gdp cpi romer_loglevel bg_loglevel std_nipa std_cpi...
    std_bg romer bg


% Define different periods
tinterval.ft_preWW1 = 1:47;
tinterval.ft_postWW2 = 80:140;
tinterval.ft_60s = 94:103;
tinterval.ft_post80s = 124:140;
tinterval.ft_preWW2 = 1:79;
period = {'preWW1','postWW2','post80s','60s','preWW2'};
save('matFiles/tinterval','period','tinterval');


%% Load results and compute standard deviations for Table 1
clear all; clc;
% Define subsets
subset_load= {'all';'nonagr';'real';'nom';'nonagr_nom'};
tv_spec = {'intermediate';'constant'};
save('matFiles/subset_load','subset_load');
save('matFiles/tv_spec','tv_spec');

for px=1:size(tv_spec,1)
    save('matFiles/px','px');
    load('matFiles/subset_load');
    
for ix =1:size(subset_load,1)
    load('matFiles/px');
    load('matFiles/tinterval');
    load('matFiles/tv_spec');
    load('matFiles/subset_load');
    load('data/data_gdp');
    
    if strcmp(tv_spec{px},'intermediate')==1
    filename_sub = char(['matFiles/' tv_spec{px} '/results_52_1867_2006_',subset_load{ix},'_',tv_spec{px}]);
    else
    filename_sub = char(['matFiles/' tv_spec{px} '/results_52_1867_2006_',subset_load{ix},'_',tv_spec{px}]);    
    end
    
    load(filename_sub);
    
    disp([subset_load{ix},'_',tv_spec{px}]);
    
    eval(['T_pre=size(results.ft(:,tinterval.ft_' period{5} ',:),2);']);
    eval(['T_post=size(results.ft(:,tinterval.ft_' period{2} ',:),2);']);
    eval(['ft_preWW2=results.ft(:,tinterval.ft_' period{5} ' ,:);']);
    eval(['ft_postWW2=results.ft(:,tinterval.ft_' period{2} ' ,:);']);
    ratio_ft = std(ft_postWW2,0,2)./std(ft_preWW2,0,2);
    
    if ix>3
        ft_stand_pre = ft_preWW2*std_cpi./(repmat(ratio_ft,[1,T_pre,1]).*repmat(std(ft_preWW2,0,2), [1,T_pre,1]));
        ft_stand_post = ft_postWW2*std_cpi./repmat(std(ft_postWW2,0,2), [1,size(ft_postWW2,2),1]);
    else
        ft_stand_pre = ft_preWW2*std_nipa./(repmat(ratio_ft,[1,T_pre,1]).*repmat(std(ft_preWW2,0,2), [1,T_pre,1]));
        ft_stand_post = ft_postWW2*std_nipa./repmat(std(ft_postWW2,0,2), [1,size(ft_postWW2,2),1]);
    end
    ft_stand = cat(2,ft_stand_pre,ft_stand_post);
    
%     for px=1:size(period,2)-1 % without preWW2
%         eval(['ft_all.' subset_load{ix} '_' period{px} '=ft_stand(:,tinterval.ft_' period{px} ',:);']);
%     end
%     
    eval(['perstd.' subset_load{ix} '_' period{1} '_'  tv_spec{px} '_med' '= median(std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2),3);']);
    eval(['perstd.' subset_load{ix} '_' period{1} '_'  tv_spec{px} '_std' '= std(std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2),0,3);']);
    
    eval(['perstd.' subset_load{ix} '_' period{2} '_'  tv_spec{px} '_med' '= median(std(ft_stand(:,tinterval.ft_' period{2} ',:),0,2),3);']);
    eval(['perstd.' subset_load{ix} '_' period{2} '_'  tv_spec{px} '_std' '= std(std(ft_stand(:,tinterval.ft_' period{2} ',:),0,2),0,3);']);
    
    eval(['temp=std(ft_stand(:,tinterval.ft_' period{2} ',:),0,2)./std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2);']);
    eval(['perstd.' subset_load{ix} '_'  tv_spec{px} '_postWW2preWW1_med' '= median(temp,3);']);
    eval(['perstd.' subset_load{ix} '_'  tv_spec{px} '_postWW2preWW1_std' '= std(temp,0,3);']);
    
    eval(['temp=std(ft_stand(:,tinterval.ft_' period{3} ',:),0,2)./std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2);']);
    eval(['perstd.' subset_load{ix} '_'  tv_spec{px}  '_post80spreWW1_med' '= median(temp,3);']);
    eval(['perstd.' subset_load{ix} '_'  tv_spec{px}  '_post80spreWW1_std' '= std(temp,0,3);']);
    
    eval(['temp=std(ft_stand(:,tinterval.ft_' period{4} ',:),0,2)./std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2);']);
    eval(['perstd.' subset_load{ix} '_'  tv_spec{px} '_60spreWW1_med' '= median(temp,3);']);
    eval(['perstd.' subset_load{ix} '_'  tv_spec{px} '_60spreWW1_std' '= std(temp,0,3);']);
    
    save('matFiles/factors_table1','perstd');%'ft_all',
    
    clear all;
    load('matFiles/factors_table1');
end
end
%% Create Excel-file of results
load('matFiles/subset_load');
load('matFiles/factors_table1');

perstd_c = struct2cell(perstd);
for ix=1:size(subset_load,1)*2
perstd_mat(ix,:) = cell2mat(perstd_c(1+10*(ix-1):10+10*(ix-1)))'.*[ones(1,4)*100 ones(1,6)];
end


names_col = {'','medPreWW1','stdPreWW1','medPostWW2','stdPostWW2',...
    'medPostWW2PreWW1','stdPostWW2PreWW1','medPost80sPreWW1','stdPost80sPreWW1',...
    'med60sPreWW1','std60sPreWW1'};
names_row = [subset_load;subset_load];

xlswrite('tables/volaComparison_final', names_col, 'Table1_fromMatlab', 'A1');
xlswrite('tables/volaComparison_final', names_row, 'Table1_fromMatlab', 'A2');
xlswrite('tables/volaComparison_final', perstd_mat, 'Table1_fromMatlab', 'B2');
